package demo2;

public class Test2 {

    public boolean isPalindrome(String s) {
        s = s.toLowerCase();
        int left = 0;
        int right = s.length()-1;

        while (left < right) {
            while (left < right && !isValidChar(s.charAt(left))){
                left++;
            }
            //left是有效的字符了
            while (left < right && !isValidChar(s.charAt(right))){
                right--;
            }
            //right是有效的字符了
            if(s.charAt(left) != s.charAt(right)) {
                return false;
            }else {
                left++;
                right--;
            }
        }
        return true;
    }

    private boolean isValidChar(char ch) {
        if(Character.isDigit(ch) || Character.isLetter(ch)) {
            return true;
        }
        return false;
        /*if(ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z') {
            return true;
        }
        return false;*/
    }


    public int firstUniqChar(String s) {
        int[] count = new int[125];
        for(int i = 0;i < s.length();i++) {
            char ch = s.charAt(i);
            count[ch]++;
        }

        for(int i = 0;i < s.length();i++) {
            char ch = s.charAt(i);
            if(count[ch] == 1) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("abc");

        StringBuffer stringBuffer = new StringBuffer("abc");
        stringBuffer.append("fafsafsafa");


        System.out.println(stringBuilder);


        stringBuilder.append("123").append(10);
        stringBuilder.append(19.9);

        System.out.println(stringBuilder);//此时 不会产生临时对象

        String ret = stringBuilder.toString();


        /*stringBuilder.reverse();
        System.out.println(stringBuilder);*/



    }



    public static void main4(String[] args) {
        long start = System.currentTimeMillis();
        String s = "";
        for(int i = 0; i < 10000; ++i){
            s += i;
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);



        start = System.currentTimeMillis();
        StringBuffer sbf = new StringBuffer("");
        for(int i = 0; i < 10000; ++i){
            sbf.append(i);
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);



        start = System.currentTimeMillis();
        StringBuilder sbd = new StringBuilder();
        for(int i = 0; i < 10000; ++i){
            sbd.append(i);
        }

        end = System.currentTimeMillis();
        System.out.println(end - start);

    }
    public static void main1(String[] args) {
        String str = "hello";
        str += "abc";
        /*for (int i = 0; i < 10; i++) {
            str += "abc"; 不建议 在循环当中  使用+对字符串进行拼接
        }*/
        System.out.println(str);
    }

    public static void main2(String[] args) {
        String str = "hello";
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(str);
        stringBuilder.append("abc");
        str = stringBuilder.toString();
        System.out.println(str);
    }
}
